package net.wds.ssbus.modules.ssbus.util;

import net.wds.ssbus.modules.ssbus.pojo.TbSsbusChannelEntrty;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;

public class GetIpAndPortUtils {
    //获取ip
    public static String getLocalIP() {
        String localIP = "127.0.0.1";
        try {
            OK:
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp()) {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (address instanceof Inet4Address) {
                        localIP = address.getHostAddress();
                        break OK;
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return localIP;
    }

    //通过request获取ip
    public static String getIp() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServerName();
    }

    //通过request获取端口
    public static String getLocalPort() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServerPort() + "";
    }


    public static String getIpAndPort() {
        return getLocalIP() + ":" + getLocalPort();
    }


    public static boolean connect(String server, int servPort) throws IOException {


        InetAddress ad = InetAddress.getByName(server);
        boolean state = ad.isReachable(1000);//测试是否可以达到该地址 ,判断ip是否可以连接 1000是超时时间
        if (state) {
            System.out.println("连接成功" + ad.getHostAddress());
        } else {
            System.out.println("连接失败");

            throw new IOException();
        }
        Socket socket = new Socket();
        socket.setReceiveBufferSize(8192);
        socket.setSoTimeout(1000);// socket.setSoTimeout(2000);
        SocketAddress address = new InetSocketAddress(server, servPort);
        try {
            socket.connect(address, 1000);//1.判断ip、端口是否可连接
        } catch (IOException e) {
            System.out.println("新建一个 socket server " + servPort + "连接失败");
            return false;
        }
        System.out.println("新建一个socket");
        return true;
    }

    public static boolean testing(TbSsbusChannelEntrty tbSsbusChannelEntrty) {
        String username = tbSsbusChannelEntrty.getUsername();
        String password = tbSsbusChannelEntrty.getPassword();
        if (tbSsbusChannelEntrty.getType()==117) {

            FTPClient ftp = FtpUtils.connectFtpServer(tbSsbusChannelEntrty.getHost(), username, password);
            System.out.println("ftp" + ftp);
            if(ftp==null){
                return false;
            }else{
                return true;
            }
        } else {
            if (tbSsbusChannelEntrty.getDbtype()== 1) {
                //连接MySql数据库，用户名和密码都是root
                String url = "jdbc:mysql://" + tbSsbusChannelEntrty.getHost() + ":" + tbSsbusChannelEntrty.getPort() + "";
                try {
                    Connection con = DriverManager.getConnection(url, username, password);
                    if (con != null) {
                        System.out.println("mysql+连接数据库成功");
                        return true;
                    }
                } catch (SQLException se) {
                    System.out.println("mysql+数据库连接失败！");

            }
            } else if(tbSsbusChannelEntrty.getDbtype() == 2){
                    String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//这是要连接的数据库加载器
                    String dbURL = "jdbc:sqlserver://" + tbSsbusChannelEntrty.getHost() + ":" + tbSsbusChannelEntrty.getPort() + "";//这是要连接的端口号以及数据库名称
                    try {
                        Class.forName(driverName);
                        System.out.println("加载驱动成功");
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                        System.out.println("加载驱动失败");
                    }
                    try {
                        Connection dbConn = DriverManager.getConnection(dbURL, username, password);
                        if (dbConn != null) {
                            System.out.println("连接数据库成功");
                            return true;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();
                        System.out.println("sqlserver+数据库连接失败");
                    }
                    return false;
                }
            }
        return false;
    }
}
